﻿
using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.ModelConfiguration;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Data.SqlClient;

namespace RM.BasicLibrary.Models.Databases
{
    public class MSDatabase : BaseDatabase, IDatabase
    {
        public override string ConnectionString { get; set; }

        public class DatabaseContext : DbContext, IDisposable, IObjectContextAdapter, IDBContext
        {
            public DbSet<UserInfo> UserInfo { get; set; }
            #region 构造函数
            /// <summary>
            /// 初始化一个 使用指定数据连接名称或连接串 的数据访问上下文类 的新实例
            /// </summary>
            /// <param name="connString">连接字串</param>
            public DatabaseContext(string connString)
                : base(new SqlConnection(connString), true)
            {
                this.Configuration.AutoDetectChangesEnabled = false;
                this.Configuration.ValidateOnSaveEnabled = false;
                this.Configuration.LazyLoadingEnabled = false;
                this.Configuration.ProxyCreationEnabled = false;
              //  System.Data.Entity.Database.SetInitializer(new CreateDatabaseIfNotExists<DbContext>());
            }
            #endregion

            #region 重载
            /// <summary>
            /// 模型创建重载
            /// </summary>
            /// <param name="modelBuilder">模型创建器</param>
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
                OnModelCreate?.Invoke(modelBuilder);
                base.OnModelCreating(modelBuilder);
            }
            
            
            public Action<DbModelBuilder> OnModelCreate { get; set; }
         
            #endregion
        }
    }
    [Table("UserInfo")]
    public class UserInfo
    {
        public int UserID { get; set; }
        public string User_Code { get; set; }
        public string User_Pwd { get; set; }
        public string IcCardSerial { get; set; }
    }
}
